webUtils.nameSpace("wiseMen.platform.Dialog");
wiseMen.platform.Dialog.dialog=function(params){
	var that = this;
	var uuid=webUtils.uuid();
	params = $.extend({
		isModal : true,
		width : 0,
		height: 0,
		title:'',
		content:'',
		url:'',
		dom:'',
		size:'n',//n w
		closeAble:true,
		maxminAble:true,
		offset: '20px',
		onremove:function(){},
		onshown:function(){}
	}, params);
	
	var area="";
	if(params.width && params.height){
		area=[params.width+"px",params.height+"px"];
	}else if(params.width){
		area=params.width+"px";
	}else if(params.size == 'n'){
		area=$(window).width()*0.5+"px";
	}else if(params.size == 'w'){
		area=$(window).width()*0.8+"px";
	}
	
	if(params.dom){
		params.dom.show();
	}
	
	this.dialog=layer.open({
		id:uuid,
		type:1,
		maxmin:true,
		title:params.title,
		offset: params.offset,
		fix:false,
		closeBtn:params.closeAble?"1":false,
		maxmin:params.maxminAble,
		content:params.dom?params.dom:"<div id='dialog_"+uuid+"'><div class='modal-body' ></div><div class='modal-footer'></div></div>",
		area : area,
		cancel:function(d){
			if (params.onremove) {
                params.onremove.call(null);
            }
		},
		success:function(d){
			if (params.onshow) {
                params.onshow.call(null);
            }
		}
	});
	
	var dialogDom=$("#dialog_"+uuid);
	var modalBody=dialogDom.find(".modal-body");
	var modalFooter=dialogDom.find(".modal-footer");
	
	this.close=function(){
		layer.close(that.dialog);
	}
	this.getModalBody=function(){
		return modalBody;
	}
	
	this.createButton=function (button) {
        var $button = $('<button class="btn"></button>');
        $button.prop('id', button.id);
        $button.data('button', button);

        // Icon
        if (typeof button.icon !== 'undefined' && $.trim(button.icon) !== '') {
            $button.append(this.createButtonIcon(button.icon));
        }

        // Label
        if (typeof button.label !== 'undefined') {
            $button.append(button.label);
        }

        // Css class
        if (typeof button.cssClass !== 'undefined' && $.trim(button.cssClass) !== '') {
            $button.addClass(button.cssClass);
        } else {
            $button.addClass('btn-default');
        }

        // Button on click
        $button.on('click', {dialog: this, $button: $button, button: button}, function (event) {
            var dialog = event.data.dialog;
            var $button = event.data.$button;
            var button = $button.data('button');
            if (button.autospin) {
                $button.toggleSpin(true);
            }
            if (typeof button.action === 'function') {
                return button.action.call($button, dialog, event);
            }
        });

        // Dynamically add extra functions to $button
        //this.enhanceButton($button);

        //Initialize enabled or not
        if (typeof button.enabled !== 'undefined') {
            $button.toggleEnable(button.enabled);
        }

        return $button;
    }
	
	this.createButtonIcon=function (icon) {
        var $icon = $('<span></span>');
        $icon.addClass(icon);

        return $icon;
    }
	//init
	if(params.url){
		modalBody.load(params.url);
	}else if(params.content){
		modalBody.append(params.content);
	}
	
	if(params.buttons && params.buttons.length>0){
        this.indexedButtons = {};
        $.each(params.buttons, function (index, button) {
            if (!button.id) {
                button.id = webUtils.uuid();
            }
            var $button = that.createButton(button);
            modalFooter.append($button);
        });

	}else{
		modalFooter.hide();
	}
	if (params.onshown) {
        params.onshown.call(null,this);
    }
	return this;
};

wiseMen.platform.Dialog.prompt=function(params){
	var that = this;
	params = $.extend({
		title:null,
		callback:function(){}
	}, params);
	
	layer.prompt({
		formType:2,
		title:params.title
	}, function(value, index, elem){
		params.callback.call(that,value);
	    layer.close(index);
	});
};

wiseMen.platform.Dialog.confirm=function(params){
	var that = this;
	params = $.extend({
		msg:null,
		callback:function(){}
	}, params);
	
	layer.confirm(params.msg, {icon: 3, title:'提示'}, function(index){
		params.callback.call(that,true);
		layer.close(index);
	},function(index){
		params.callback.call(that,false);
		layer.close(index);
	});
}